package cn.hp.controller;

import cn.hp.entity.Announcement;
import cn.hp.entity.User;
import cn.hp.service.AnnouncementService;
import cn.hp.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/announcement")
public class AnnouncementController {
    @Autowired
    private AnnouncementService announcementService;

    @GetMapping
    public Result get(Announcement announcement) {
        return Result.success(announcementService.selectAnnouncements(announcement));
    }

    @RequestMapping("/page")
    public Result page(Announcement announcement, @RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        return Result.success(announcementService.selectAnnouncements(announcement, page, limit));
    }

    @DeleteMapping("/{id}")
    public Result del(@PathVariable Integer id) {
        System.out.println("id+" + id);
        int i = announcementService.deleteAnnouncementById(id);
        if (i == 1) {
            return Result.success("删除成功");
        } else {
            return Result.error("删除失败");
        }

    }

    @PostMapping
    public Result insert(@RequestBody Announcement announcement) {
        int i = announcementService.insertAnnouncement(announcement);
        if (i == 1) {
            return Result.success("添加成功");
        } else {
            return Result.error("添加失败");
        }

    }

    @PutMapping
    public Result update(@RequestBody Announcement announcement) {
        int i = announcementService.updateAnnouncement(announcement);
        if (i == 1) {
            return Result.success("修改成功");
        } else {
            return Result.error("修改失败");
        }
    }
}
